package org.tinygroup.earthworm;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

public class WormBaseContext {
	public static final String KV_SPLITER = ":";
	public static final String KV_ITEM_SPLITER = ",";
	static final int DEFAULT_LOCAL_INDEX = 1;
	static final int DEFAULT_RPC_INDEX = 1;
	String relationId;
	String traceId;
	String spanId;
	String traceName;
	long startTime;
	int milestone;
	int endTime;
	String tag;
	AtomicInteger localIdx;
	String localId;
	String errorMsg;
	Map<String, String> customInofos = new HashMap<String, String>();
	Map<String, String> localAttributes= new HashMap<String, String>();

	WormBaseContext(String _traceId, String _spanId) {
		this(_traceId, _spanId, new AtomicInteger(0));
	}

	WormBaseContext(String _traceId, String _spanId, AtomicInteger _localIdx) {
		this.localId = "";
		this.localIdx = _localIdx;
		this.traceName = "";
		this.tag = "";
		this.traceId = _traceId;
		this.spanId = _spanId;
		this.errorMsg="";
	}
	
	public String toString(){
		StringBuilder sb = new StringBuilder();
		sb.append("traceId").append(KV_SPLITER).append(traceId).append(KV_ITEM_SPLITER);
		sb.append("spanId").append(KV_SPLITER).append(spanId).append(KV_ITEM_SPLITER);
		sb.append("relationId").append(KV_SPLITER).append(relationId).append(KV_ITEM_SPLITER);
		sb.append("traceName").append(KV_SPLITER).append(traceName).append(KV_ITEM_SPLITER);
		sb.append("tag").append(KV_SPLITER).append(tag).append(KV_ITEM_SPLITER);
		sb.append("startTime").append(KV_SPLITER).append(startTime).append(KV_ITEM_SPLITER);
		sb.append("milestone").append(KV_SPLITER).append(milestone).append(KV_ITEM_SPLITER);
		sb.append("endTime").append(KV_SPLITER).append(endTime).append(KV_ITEM_SPLITER);
		sb.append("errorMsg").append(KV_SPLITER).append(errorMsg).append(KV_ITEM_SPLITER);
		sb.append("localIdx").append(KV_SPLITER).append(localIdx).append(KV_ITEM_SPLITER);
		sb.append("localId").append(KV_SPLITER).append(localId);
		return sb.toString();
	}
}
